package main

import (
	"context"
	"fmt"
	"github.com/zing-dev/general-protocol-lib/source/device"
	dts2 "github.com/zing-dev/general-protocol-lib/source/dts"
	"log"
	"time"
)

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), time.Hour)
	app := dts2.New(ctx, dts2.DTS{Id: 1, Name: "192.168.0.86", Host: "192.168.0.86"}, &dts2.Config{ChannelNum: 4})
	app.CallTypes = []dts2.CallType{dts2.CallAlarm, dts2.CallTemp, dts2.CallEvent}
	app.Run()
	go func() {
		for {
			select {
			case <-time.After(time.Second * 16):
				log.Println("----> ping")
				cancel()
			case <-time.After(time.Second * 30):
				log.Println("----> check")
			}
		}
	}()
	for {
		select {
		case <-app.Context.Done():
			app.Client.Close()
			fmt.Println("out")
			return
		case status := <-app.ChanStatus:
			if status == device.Connected {
				app.Register()
				app.SyncZones()
			}
		case temp := <-app.ChanZonesTemp:
			log.Println("temp", temp.DeviceId)
		case sign := <-app.ChanChannelSignal:
			log.Println("sign", sign.DeviceId)
		case event := <-app.ChanChannelEvent:
			log.Println("event", event.DeviceId)
		case alarm := <-app.ChanZonesAlarm:
			log.Println("alarm start", alarm.DeviceId)
			time.Sleep(time.Second * 5)
			log.Println("alarm over", alarm.DeviceId)
		}
	}
}
